﻿package ReadWriteXML;

import java.io.File;
import java.util.ArrayList;

import javax.xml.parsers.DocumentBuilder;
import javax.xml.parsers.DocumentBuilderFactory;
import javax.xml.parsers.ParserConfigurationException;

import org.w3c.dom.Document;
import org.w3c.dom.Node;
import org.w3c.dom.Element;
import org.w3c.dom.NodeList;

import com.application.Controller.ReadWriteFileController;

import android.util.Log;



public class WriteXML {


	/**
	 * This class construcs an XML document in memory using DOM.
	 * It first creates the root Order element and subsequently creates components
	 * of the order by inserting nodes to the root element.
	 */
	private static Document CreateOrderDOM(String root, Document doc)
	{
		DocumentBuilder builder = null;
		DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance();
		try
		{
			builder = factory.newDocumentBuilder();
			doc = builder.newDocument();

		}
		catch (ParserConfigurationException e)
		{
			e.printStackTrace();
		}                
		Element _root = doc.createElement(root);        
		doc.appendChild(_root);
		// Normalizing the DOM
		doc.getDocumentElement().normalize();
		return doc;
	}

	private static Document insertItemQuesion(Document doc, Node parent, CQuestion question) {
		// Insert child Item
		Element itemChild = doc.createElement("Question");
		itemChild.setAttribute("ID", ""+question.get_idQuestion());
		itemChild.setAttribute("Type", ""+question.get_type());
		parent.appendChild(itemChild);

		// Insert child length
		Node item = doc.createElement("Length");
		itemChild.appendChild(item);
		// Insert length value
		Node value = doc.createTextNode(String.valueOf(question.get_length()));
		item.appendChild(value);

		// Insert child content
		item = doc.createElement("Content");
		itemChild.appendChild(item);
		// Insert content value
		value = doc.createTextNode(question.get_content());
		item.appendChild(value);

		// Insert child time
		item = doc.createElement("Time");
		itemChild.appendChild(item);

		// Insert time value
		value = doc.createTextNode(String.valueOf(question.get_time()));
		item.appendChild(value);
		
		if(question.get_type() == 3 )//true false
		{
			item = doc.createElement("ListOption");
			itemChild.appendChild(item);

			value = doc.createTextNode("null");
			item.appendChild(value);
		}
		else if(question.get_type() == 5)//câu hỏi tự luận
		{
			item = doc.createElement("ListOption");
			itemChild.appendChild(item);

			value = doc.createTextNode("1");
			item.appendChild(value);
			
			item = doc.createElement("ListOption");
			itemChild.appendChild(item);

			value = doc.createTextNode("0");
			item.appendChild(value);

		}
		else
		{
			ArrayList<COption> listOption = new ArrayList<COption>();
			listOption = question.get_listOption();


			for(COption option : listOption)
			{
				//trường hợp nối cột
				if(option.get_content() == "")
				{
					// Insert child listOption
					Element e = doc.createElement("ListOption");
					e.setAttribute("ID", ""+option.get_idDetail());
					itemChild.appendChild(e);

					//Insert child key
					Node item2 = doc.createElement("Key");
					e.appendChild(item2);
					// Insert key value
					value = doc.createTextNode(option.get_key());
					item2.appendChild(value);

					//Insert child value 
					item2 = doc.createElement("Value");
					e.appendChild(item2);
					// Insert value value
					value = doc.createTextNode(option.get_value());
					item2.appendChild(value);
				}
				else
				{
					// Insert child listOption
					Element e = doc.createElement("ListOption");
					e.setAttribute("ID", ""+option.get_idDetail());
					itemChild.appendChild(e);

					// Insert value value
					value = doc.createTextNode(option.get_content());
					e.appendChild(value);
				}
			} 
		}
		return doc;
	}
//	private static Document insertItemQuesion(Document doc, Node parent, CQuestion question) {
//		// Insert child Item
//		Element itemChild = doc.createElement("Question");
//		itemChild.setAttribute("ID", ""+question.get_idQuestion());
//		itemChild.setAttribute("Type", ""+question.get_type());
//		parent.appendChild(itemChild);
//
//		// Insert child length
//		Node item = doc.createElement("Length");
//		itemChild.appendChild(item);
//		// Insert length value
//		Node value = doc.createTextNode(String.valueOf(question.get_length()));
//		item.appendChild(value);
//
//		// Insert child content
//		item = doc.createElement("Content");
//		itemChild.appendChild(item);
//		// Insert content value
//		value = doc.createTextNode(question.get_content());
//		item.appendChild(value);
//
//		// Insert child time
//		item = doc.createElement("Time");
//		itemChild.appendChild(item);
//
//		// Insert time value
//		value = doc.createTextNode(String.valueOf(question.get_time()));
//		item.appendChild(value);
//		
//		if(question.get_type() == 3 || question.get_type() == 5)//câu hỏi tự luận hoặc true false
//		{
//			item = doc.createElement("ListOption");
//			itemChild.appendChild(item);
//
//			value = doc.createTextNode("null");
//			item.appendChild(value);
//		}
//		else
//		{
//			ArrayList<COption> listOption = new ArrayList<COption>();
//			listOption = question.get_listOption();
//
//
//			for(COption option : listOption)
//			{
//				//trường hợp nối cột
//				if(option.get_content() == "")
//				{
//					// Insert child listOption
//					Element e = doc.createElement("ListOption");
//					e.setAttribute("ID", ""+option.get_idDetail());
//					itemChild.appendChild(e);
//
//					//Insert child key
//					Node item2 = doc.createElement("Key");
//					e.appendChild(item2);
//					// Insert key value
//					value = doc.createTextNode(option.get_key());
//					item2.appendChild(value);
//
//					//Insert child value 
//					item2 = doc.createElement("Value");
//					e.appendChild(item2);
//					// Insert value value
//					value = doc.createTextNode(option.get_value());
//					item2.appendChild(value);
//				}
//				else
//				{
//					// Insert child listOption
//					Element e = doc.createElement("ListOption");
//					e.setAttribute("ID", ""+option.get_idDetail());
//					e.setAttribute("Check", "0");
//					e.setAttribute("Correct", "0");
//					e.setTextContent(option.get_content());
//					item.appendChild(e);
//				}
//			} 
//		}
//		return doc;
//	}

	private static Document insertItemResult(Document doc, Node parent, CResult result)
	{
		// Insert child Item
		Element itemChild = doc.createElement("Result");
		itemChild.setAttribute("IDQuestion", ""+ result.getIDQuestion());
		itemChild.setAttribute("Type", ""+ result.getType());
		parent.appendChild(itemChild);

		// Insert child type
		Node item = doc.createElement("ListChoice");
		itemChild.appendChild(item);
		Node item2 = null;
		Node value = null;
		switch(result.getType())
		{
		case 2: //multi choice
		{
			int length = result.getChoice().size();
			for(int i = 0; i<length; i++ )
			{
				// Insert child type
				item2 = doc.createElement("Choice");
				item.appendChild(item2);
				// Insert type value
				value = doc.createTextNode(result.getChoice().get(i));
				item2.appendChild(value);
			}
			break;
		}
		case 4: //nối cột
		{
			int length = result.getPairChoice().size();
			for(int i = 0; i<length; i++ )
			{
				// Insert tag PairChoice
				item2 = doc.createElement("PairChoice");
				item.appendChild(item2);

				//insert tag value 1
				Node item3 = doc.createElement("value");
				item2.appendChild(item3);

				// Insert value1
				value = doc.createTextNode(result.getPairChoice().get(i).get(0));
				item3.appendChild(value);

				//insert tag value 2
				item3 = doc.createElement("value");
				item2.appendChild(item3);

				// Insert value1
				value = doc.createTextNode(result.getPairChoice().get(i).get(1));
				item3.appendChild(value);
			}
			break;
		}
		default:
		{
			value = doc.createTextNode(result.getChoice().get(0));
			item.appendChild(value);
		}

		}
		return doc;

	}

	private static Document insertItemLearner(Document doc, Node parent, CLearner learner)
	{
		// Insert child Item
		Element itemChild = doc.createElement("Learner");
		itemChild.setAttribute("ID", ""+ learner.get_id());
		itemChild.setAttribute("Username", ""+ learner.get_userName());
		itemChild.setAttribute("Pass", ""+ learner.get_pass());
		parent.appendChild(itemChild);

		// Insert fullname
		Node item = doc.createElement("Fullname");
		itemChild.appendChild(item);

		Node value = doc.createTextNode(learner.get_fullName());
		item.appendChild(value);

		// Insert Birthday
		item = doc.createElement("Birthday");
		itemChild.appendChild(item);

		value = doc.createTextNode(learner.get_birthday());
		item.appendChild(value);

		// Insert Address
		item = doc.createElement("Address");
		itemChild.appendChild(item);

		value = doc.createTextNode(learner.get_address());
		item.appendChild(value);

		// Insert Phone
		item = doc.createElement("Phone");
		itemChild.appendChild(item);

		value = doc.createTextNode(learner.get_phone());
		item.appendChild(value);

		// Insert IDClass
		item = doc.createElement("IDClass");
		itemChild.appendChild(item);

		value = doc.createTextNode(String.valueOf(learner.get_idClass()));
		item.appendChild(value);

		return doc;
	}

	 private static Document insertItemIP(Document doc, Node parent, int id, String userName, String fullName, int idclass, String ip)
	  {
		// Insert child Item
			Element e = doc.createElement("Learner");
			parent.appendChild(e);

			e.setAttribute("UserName", userName);
			e.setAttribute("ID", ""+id);
			e.setAttribute("FullName", fullName);
			e.setAttribute("IDClass", idclass+"");
			
			//tạo node ip
			Node nodeChild = doc.createElement("IP");
			e.appendChild(nodeChild);

			Node vaule = doc.createTextNode(ip);
			nodeChild.appendChild(vaule);
			return doc;
	  }
	
	private static Document insertItemIP(Document doc, Node parent, int id, String ip)
	{
		// Insert child Item
		Element e = doc.createElement("Learner");
		parent.appendChild(e);

		e.setAttribute("ID", ""+id);
		Node nip = doc.createElement("IP");
		e.appendChild(nip);

		Node vaule = doc.createTextNode(ip);
		nip.appendChild(vaule);

		return doc;
	}

	/**
	 * 
	 * @param doc
	 * @param parent
	 * @param userName
	 * @param ip
	 * @param fullName
	 * @return
	 */
	private static Document insertItemIP(Document doc, Node parent, String userName, String ip, String fullName)
	{
		// Insert child Item
		Element e = doc.createElement("Learner");
		parent.appendChild(e);

		e.setAttribute("UserName", userName);

		//tạo node ip
		Node nodeChild = doc.createElement("IP");
		e.appendChild(nodeChild);

		Node vaule = doc.createTextNode(ip);
		nodeChild.appendChild(vaule);

		//tạo node fullname
		nodeChild = doc.createElement("FullName");
		e.appendChild(nodeChild);

		vaule = doc.createTextNode(fullName);
		nodeChild.appendChild(vaule);

		return doc;
	}


	private static Document insertItemTestList(Document doc, Node parent, String title, int idPart, int idDetailLearning, int idExercise, String fNameTest)
	{
		// Insert child Item
		Element e = doc.createElement("Test");
		parent.appendChild(e);

		e.setAttribute("IDPart", String.valueOf(idPart));
		e.setAttribute("IDDetailLearning", String.valueOf(idDetailLearning));
		e.setAttribute("IDExercise", String.valueOf(idExercise));
		//tạo node ip
		Node nodeChild = doc.createElement("NameTest");
		e.appendChild(nodeChild);

		Node vaule = doc.createTextNode(fNameTest);
		nodeChild.appendChild(vaule);

		//tạo node fullname
		nodeChild = doc.createElement("Title");
		e.appendChild(nodeChild);

		vaule = doc.createTextNode(title);
		nodeChild.appendChild(vaule);

		return doc;
	}
	
	/**
	 * Hàm tạo root cho Document, đây là hàm quan trọng, phải insert root rồi mới có thể insert các tag
	 * @param doc
	 * @param path
	 * @param root
	 * @return
	 */
	public static Document insertRoot(Document doc, String path, String root)
	{
		// Build new DOM Document
		doc = CreateOrderDOM(root, doc);
		//kiem tra fileName da ton tai hay chua
		File f = new File(path);
		if(!f.exists())
		{
			// Print XML content
			DOMUltil.printDOM(doc);

			// Write to a file
			DOMUltil.writeXmlToFile(path,doc);	             
		}  
		return doc;
	}

	/**
	 * Hàm tạo một tag kèm theo giá trị đi kèm
	 * @param doc
	 * @param path
	 * @param parent: là node cha
	 * @param tagName
	 * @param val
	 * @return
	 */
	public static Document insertVauleTagNode(Document doc, String path, Node parent, String tagName, String val)
	{
		//insert tag
		Node itemChild = doc.createElement(tagName);
		parent.appendChild(itemChild);

		Node value = doc.createTextNode(val);
		itemChild.appendChild(value);

		DOMUltil.writeXmlToFile(path, doc);
		return doc;
	}

	/**
	 * Hàm tạo một tag kèm theo giá trị đi kèm
	 * @param doc
	 * @param path
	 * @param parent: tên node cha
	 * @param tagName
	 * @param val
	 * @return
	 */
	public static Document insertValueTagNode(Document doc, String path, String parent, String tagName, String val)
	{
		//insert tag
		Node root = doc.getElementsByTagName(parent).item(0);
		Node itemChild = doc.createElement(tagName);
		root.appendChild(itemChild);

		Node value = doc.createTextNode(val);
		itemChild.appendChild(value);

		DOMUltil.writeXmlToFile(path,doc);
		return doc;

	}

	/**
	 * Hàm tạo một tag, không đi kèm giá trị
	 * @param doc
	 * @param path
	 * @param parent: tên node cha
	 * @param tagName
	 * @return
	 */
	public static Document insertTagNode(Document doc, String path, String parent, String tagName)
	{
		//insert tag
		Node root = doc.getElementsByTagName(parent).item(0);
		Node itemChild = doc.createElement(tagName);
		root.appendChild(itemChild);

		DOMUltil.writeXmlToFile(path,doc);
		return doc;
	}

	/**
	 * Hàm tạo một tag, không đi kèm giá trị
	 * @param doc
	 * @param path
	 * @param parent: là node cha
	 * @param tagName
	 * @return
	 */
	public static Document insertTagNode(Document doc, String path, Node parent, String tagName)
	{
		//insert tag
		Node itemChild = doc.createElement(tagName);
		parent.appendChild(itemChild);

		DOMUltil.writeXmlToFile(path,doc);
		return doc;
	}

	/**
	 * Hàm tạo một element đi kèm với các thuộc tính và giá trị của nó
	 * @param doc
	 * @param path
	 * @param parent: là node cha
	 * @param tagName
	 * @param attribute: mảng lưu tên các thuộc tính
	 * @param value: mảng lưu giá trị tương ứng của các thuộc tính
	 * @return
	 */
	public static Document insertTagElement(Document doc, String path, Node parent, String tagName, ArrayList<Object> attribute, ArrayList<Object> value)
	{
		//tao element tag
		Element e = doc.createElement(tagName);

		//khoi tao các giá trị attribute
		int n= attribute.size();
		for(int i = 0; i< n; i++)
			e.setAttribute(attribute.get(i).toString(), value.get(i).toString());

		parent.appendChild(e);
		DOMUltil.writeXmlToFile(path,doc);
		return doc;
	}

	
	public static Document insertTagElement(Document doc, String path, String parent, String tagName, ArrayList<Object> attribute, ArrayList<Object> value, ArrayList<Object> tagChildNode, ArrayList<Object> valueChildNode)
	{
		Node nparent = doc.getElementsByTagName(parent).item(0);
		//tao element tag
		Element e = doc.createElement(tagName);

		//khoi tao các giá trị attribute
		int n= attribute.size();
		for(int i = 0; i< n; i++)
			e.setAttribute(attribute.get(i).toString(), value.get(i).toString());

		nparent.appendChild(e);
		
		n = tagChildNode.size();
		for(int i = 0; i < n; i++)
		{
			Node child = doc.createElement(tagChildNode.get(i).toString());
			child.setTextContent(valueChildNode.get(i).toString());
			e.appendChild(child);
		}
		DOMUltil.writeXmlToFile(path,doc);
		return doc;
	}
	/**
	 * Hàm xoá một element dựa vào các thuộc tính và giá trị của nó
	 * @param doc
	 * @param path
	 * @param tagName
	 * @param attribute: mảng lưu tên các thuộc tính
	 * @param value: mảng lưu giá trị tương ứng của các thuộc tính
	 * @return
	 */
	public static Document deleteTagElement(Document doc, String path, String tag, ArrayList<Object> attribute, ArrayList<Object> value)
	{
		NodeList resList = doc.getElementsByTagName(tag);
		DOMUltil.printDOM(doc);
		int n = resList.getLength();
		boolean flag = true;
		int length = attribute.size();
		for(int i = 0; i< n; i++)
		{
			Element e = (Element)resList.item(i);
			for(int j = 0; j < length; j++)
				if(e.getAttribute(attribute.get(j).toString()).equals(value.get(j).toString()) == false)
				{	
					flag = false;
					break;
				}
			if(flag == true)
			{
				Node parent = e.getParentNode();
				parent.removeChild(e);
				break;
			}
			flag = true;
		}
		DOMUltil.writeXmlToFile(path,doc);
		//Element e = (Element)resList.item(index);
		return doc;
	}
	public static Document insertTagElement(Document doc, String path, Node parent, String tagName, String attribute, String value)
	{
		//tao element tag
		Element e = doc.createElement(tagName);

		//khoi tao các giá trị attribute
		e.setAttribute(attribute, value);

		parent.appendChild(e);
		DOMUltil.writeXmlToFile(path,doc);
		return doc;
	}

	/**
	 * Temp
	 * @param doc
	 * @param path
	 * @param parent
	 * @param tagName
	 * @param attribute
	 * @param value
	 * @param tagNode
	 * @param valueNode
	 * @return
	 */
	public static Document insertTagElement(Document doc, String path, String parent, String tagName, String attribute, Object value, String tagNode, String valueNode)
	{
		//tao element tag
		Element e = doc.createElement(tagName);

		//khoi tao các giá trị attribute
		e.setAttribute(attribute, value.toString());
		
		Node p = doc.getElementsByTagName(parent).item(0);
		p.appendChild(e);
		
		doc = insertVauleTagNode(doc, path, e, tagNode, valueNode);		
		DOMUltil.writeXmlToFile(path,doc);
		return doc;
	}

	public static Document deleteTagElement(Document doc, String path, String tag, String attribute, String value)
	{
		NodeList resList = doc.getElementsByTagName(tag);
		DOMUltil.printDOM(doc);
		int n = resList.getLength();
		for(int i = 0; i< n; i++)
		{
			Element e = (Element)resList.item(i);
			if(e.getAttribute(attribute).equals(value))
			{
				Log.d("PEERDROID","delete note");
				Node parent = e.getParentNode();
				parent.removeChild(e);
				break;
			}
		}
		DOMUltil.writeXmlToFile(path,doc);
		//Element e = (Element)resList.item(index);
		return doc;
	}
	
	/**
	 * Hàm insert một câu hỏi vào cấu trúc xml
	 * @param doc
	 * @param path
	 * @param ques
	 * @return
	 */
	public static Document insertQuestion(Document doc, String path, CQuestion ques)
	{
		if(doc != null)
		{
			Node item = doc.getElementsByTagName("QuestionList").item(0);
			DOMUltil.printDOM(item);
			doc = insertItemQuesion(doc, item,ques);
			//DOMUtil1.writeXmlToFile(fileName,document);
		}

		DOMUltil.writeXmlToFile(path,doc);
		return doc;

	}

	/**
	 * Hàm xoá một câu trả lời dựa vào id và type
	 * @param doc
	 * @param path
	 * @param id
	 * @param type
	 * @return
	 */
	public static Document deleteResult(Document doc, String path, int id, int type)
	{
		if(doc != null)
		{
			ArrayList<Object> attribute = new ArrayList<Object>();
			ArrayList<Object> value = new ArrayList<Object>();
			attribute.add("Type");
			attribute.add("IDQuestion");
			value.add(type);
			value.add(id);
			doc = deleteTagElement(doc, path, "Result", attribute, value);
		}
		//DOMUltil.writeXmlToFile(fName,doc);
		return doc;
	}

	/**
	 * hàm thêm một câu trả lời vào cấu trúc xml
	 * @param doc
	 * @param path
	 * @param res
	 * @return
	 */
	public static Document insertResult(Document doc, String path, CResult res)
	{
		if(doc != null)
		{
			Node item = doc.getElementsByTagName("ResultList").item(0);
			DOMUltil.printDOM(item);
			doc = insertItemResult(doc, item,res);
			//DOMUtil1.writeXmlToFile(fileName,document);
		}

		DOMUltil.writeXmlToFile(path,doc);
		return doc;
	}

	/**
	 * Hàm update một result cụ thể
	 * @param doc
	 * @param path
	 * @param res
	 * @return
	 */
	public static Document updateResult(Document doc, String path, CResult res)
	{
		if(doc != null)
		{
			Node item = doc.getElementsByTagName("ResultList").item(0);
			DOMUltil.printDOM(item);
			doc = deleteResult(doc, path, res.getIDQuestion(), res.getType());
			doc = insertItemResult(doc, item,res);
			//DOMUtil1.writeXmlToFile(fileName,document);
		}

		DOMUltil.writeXmlToFile(path,doc);
		return doc;
	}

	public static void copyResult(String pathQues, String pathRes)
	{
		Document docQues = null;
		Document docRes = null;
		docQues = ReadXML.BuildXML(pathQues);
		//docRes = BuildXML(fNameRes);
		Node node = docQues.getElementsByTagName("IDDetailLearning").item(0);
		String idDeatail = node.getTextContent();
		node = docQues.getElementsByTagName("Title").item(0);
		String title = node.getTextContent();
		docRes = insertRoot(docRes, pathRes, "ResultList");
		docRes = insertValueTagNode(docRes, pathRes, "ResultList", "IDDetailLearning", idDeatail);
		docRes = insertValueTagNode(docRes, pathRes, "ResultList", "Title", title);
		ArrayList<CResult> resList = ReadXML.readResult(docQues, pathQues);
		for(CResult res : resList)
		{
			docRes = insertResult(docRes, pathRes, res);
		}

	}
	/**
	 * Hàm insert thông tin học viên: ip và id
	 * @param doc
	 * @param path
	 * @param id
	 * @param ip
	 * @return
	 */
	public static Document insertIP(Document doc, String path, int id, String ip)
	{
		if(doc != null)
		{
			Node item = doc.getElementsByTagName("IPList").item(0);
			DOMUltil.printDOM(item);
			doc = insertItemIP(doc, item, id, ip);
			//DOMUtil1.writeXmlToFile(fileName,document);
		}

		DOMUltil.writeXmlToFile(path,doc);
		return doc;
	}

	/**
	 * Hàm insert thông tin học viên: userName, ip, fullName
	 * @param doc
	 * @param path
	 * @param userName
	 * @param ip
	 * @param fullName
	 * @return
	 */
	public static Document insertIP(Document doc, String path, String userName, String ip, String fullName)
	{
		if(doc != null)
		{
			Node item = doc.getElementsByTagName("LearnerList").item(0);
			DOMUltil.printDOM(item);
			doc = insertItemIP(doc, item, userName, ip, fullName);
			//DOMUtil1.writeXmlToFile(fileName,document);
		}
		DOMUltil.writeXmlToFile(path,doc);
		return doc;
	}

	/**
	 * ham insert  mot member xuong file xml
	 * @param doc
	 * @param path
	 * @param mem
	 * @return
	 */
	public static Document insertMember(Document doc, String path, CMember mem)
	{
		
		if(doc != null)
		{
			Log.d("PEERDROID","vao insermember, path: " + path);
			Node item = doc.getElementsByTagName("ListLearner").item(0);
			DOMUltil.printDOM(item);
			doc = insertItemIP(doc, item, mem.get_userName().toString(), mem.get_ip().toString(), mem.get_fullName().toString());
			//DOMUtil1.writeXmlToFile(fileName,document);
		}

		DOMUltil.writeXmlToFile(path,doc);
		return doc;
	}


	public static Document insertResultTest(String path, ArrayList<ArrayList<Object>> resultTest)
	{
		path = ReadWriteFileController.DIRECTORYTESTCONTENT + path + ".xml";
		Log.d("PEERDROID","path: dd: " + path);
		// Insert child Item
		Document doc = ReadXML.BuildXML(path);
		DOMUltil.printDOM(doc);
		if(doc != null)
		{
			Log.d("PEERDROID","vào doc !=null");
			Node parent = doc.getElementsByTagName("ResultTest").item(0);
			Log.d("PEERDROID","parent " + parent.toString());
			int length = resultTest.size();
			Log.d("PEERDROID","length" + length);
			for(int i = 0; i < length; i++)
			{
				Log.d("PEERDROID","i: " + i);
				Element res = doc.createElement("Result");
				parent.appendChild(res);
				Log.d("PEERDROID","qua append chile");
				res.setAttribute("IDQuestion", ""+ resultTest.get(i).get(0));
				Log.d("PEERDROID","ID Question" + resultTest.get(i).get(0));
				res.setAttribute("Type", ""+ resultTest.get(i).get(1));
				Log.d("PEERDROID","qua set attibute type");
				int size = resultTest.get(i).size();
				Log.d("PEERDROID","size" + size);
				for(int j = 2; j < size; j++)
				{
					Log.d("PEERDROID","vao i:" +i + "j:" + j);
					Node value = doc.createElement("value");
					value.setTextContent(""+resultTest.get(i).get(j));
					res.appendChild(value);
					
				}
			}
			DOMUltil.writeXmlToFile(path, doc);
			
			
			
			
			
			
			
			
			
			
			
			
			
			
			
			
			
			
			
			
			
			
			
			
			
			
			
			
			
			
			
			
			
			
			
			
			
			
			
			
			
			
			
			
			
			
			
			
			
			
			
			
			
			
			
			
			
			
			
			
			
			
			
			
			
			
			
			
			
			
			
			
			
			
			
			
			
			
			
			
			
			
			
			
			
			
			
			
			
			
			
			
			
			
			
			
			
			
			
			
			
			
			
			
			
			
			
			
			
			
			
			
			
			
			
			
			
			
			
			
			
			
			
			
			
			
			
			
		}
		Log.d("PEERDROID","thoat insert");
		return doc;
	}
	/**
	 * Hàm insert thông tin học viên: CLearner
	 * @param doc
	 * @param path
	 * @param learner
	 * @return
	 */
	public static Document insertLearner(Document doc, String path, CLearner learner)
	{

		if(doc != null)
		{
			Node item = doc.getElementsByTagName("LearnerList").item(0);
			DOMUltil.printDOM(item);
			doc = insertItemLearner(doc, item, learner);
			//DOMUtil1.writeXmlToFile(fileName,document);
		}

		DOMUltil.writeXmlToFile(path,doc);
		return doc;
	}

	public static Document insertTestList(Document doc, String path, String title, int idPart, int idDetailLearning, int idExercise, String fNameTest)
	{

		if(doc != null)
		{
			Node item = doc.getElementsByTagName("TestList").item(0);
			DOMUltil.printDOM(item);
			doc = insertItemTestList(doc, item, title, idPart, idDetailLearning, idExercise, fNameTest);
			//DOMUtil1.writeXmlToFile(fileName,document);
		}

		DOMUltil.writeXmlToFile(path,doc);
		return doc;
	}
	
	public static Document insertIP(Document doc, String fName, int id, String userName, String fullName, int idclass, String ip)
	{
		if(doc != null)
        {
            Node item = doc.getElementsByTagName("IPList").item(0);
            DOMUltil.printDOM(item);
            doc = insertItemIP(doc, item, id, userName, fullName, idclass, ip);
        }
        
        DOMUltil.writeXmlToFile(fName,doc);
        return doc;
	}
	
	public static Document deleteIPFromUsername(Document doc, String fName, String sUsername)
	{
		if(doc != null)
		{
			NodeList resList = doc.getElementsByTagName("Learner");
			int n = resList.getLength();
			for(int i = 0; i< n; i++)
			{
				Element e = (Element)resList.item(i);
				if(e.getAttribute("UserName").equals(sUsername))
				{
					Node parent = e.getParentNode();
				  	parent.removeChild(e);
				}
			}					
		}
		DOMUltil.writeXmlToFile(fName,doc);
		return doc;
	}
	public static Document deleteIP(String fName, String ip)
	{
		Document doc = ReadXML.BuildXML(fName);
		if(doc != null)
		{
			NodeList ipList = doc.getElementsByTagName("Learner");
			int n = ipList.getLength();
			for(int i = 0; i< n; i++)
			{
				Node noteip = ipList.item(i);
				NodeList nip = noteip.getChildNodes(); 
				if(nip.item(0).getTextContent().equals(ip))
					noteip.getParentNode().removeChild(noteip);
			}
			
		}
		DOMUltil.writeXmlToFile(fName,doc);
		return doc;
	}
//	public static Document WriteQuestion(String path, ArrayList<ArrayList<String>> quesList)
//	{
//		Document doc = null;
//		doc = insertRoot(doc, path, "QuestionList");
//		int n = quesList.size();
//		for(int i = 0; i < n; i++)
//		{
//			Node root = doc.getElementsByTagName("QuestionList").item(0);
//			Node parent = doc.createElement("Question");
//			root.appendChild(parent);
//			
//			int type = Integer.valueOf(quesList.get(i).get(0));
//			
//			Node child = doc.createElement("Type");
//			parent.appendChild(child);
//			
//			Node value = doc.createTextNode(quesList.get(i).get(0));
//			child.appendChild(value);
//			
//			child = doc.createElement("Content");
//			parent.appendChild(child);
//			
//			value = doc.createTextNode(quesList.get(i).get(0));
//			child.appendChild(value);
//			
//			Node optList = doc.createElement("OptionList");
//			parent.appendChild(optList);
//			int length = quesList.get(i).size();
//			switch (type) {
//			case 5:
//			{
//				for(int j = 0; j< length; j++)
//				{
//					child = doc.createElement("Option");
//					parent.appendChild(child);
//
//					value = doc.createTextNode(quesList.get(i).get(2+j));
//					child.appendChild(value);
//				}
//				break;
//			}
//			default:
//			{
//				for(int j = 0; j< length; j+=2)
//				{
//					Element e = doc.createElement("Option");
//					optList.appendChild(e);
//					
//					e.setAttribute("isAnswer", quesList.get(i).get(2+j));
//					e.setTextContent(quesList.get(i).get(2+j));
//				}
//				break;	
//			}
//			}
//			
//		}
//		return doc;
//	}
}

